//检验用户是否登录
define('check_log', ['cookie', 'jquery'], function(cookie, $){
	function nameLength(name){
		var realLength = 0;
		for (var i = 0; i < name.length; i++) {
			var charCode = name.charCodeAt(i);
			//字符为非中文
			if (charCode >= 0 && charCode <= 128){
				realLength += 1;
			}
			else{
				realLength += 2;
			}
			if (realLength > 13){
				name = name.substr(0, i) + '..';
				break;
			}
		}
		return name;
	};

	return function(){	//用cookie判断是否已登录
		if (cookie.getCookie('userId') != ''){	//已登录
			//显示用户按钮 + 头像
			$('#top_bar .user').css('display', 'block');

			var name = cookie.getCookie('name');
			name = nameLength(name);
			$('#top_bar .user .userName').text(name);

			var avatar = cookie.getCookie('avatarUrl');
			if (avatar != ''){
				var $img = $('#top_bar .user .log_avatar img');
				$img.attr('src', avatar);
			}
		}	
		else{	//未登录
			$('#top_bar .log_reg').css('display', 'inline');
		}
	};
});


